package com.mz.ozy.utils;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.mz.ozy.bean.DbBean;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.constants.Constants;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class AddrDbManager {
    private static DBManager2 dbManager;
    private static SQLiteDatabase sqLiteDatabase;
    public static ArrayList<DbBean> options1Items = new ArrayList<>();
    public static ArrayList<ArrayList<DbBean>> options2Items = new ArrayList<>();
    public static ArrayList<ArrayList<ArrayList<DbBean>>> options3Items = new ArrayList<>();
    public static List<DbBean> cbeans = new ArrayList<>();
    public static boolean isLoadAddress;

    /**
     * 解析地址数据
     */
    public static void loadAddressData(Context context) {
        dbManager = new DBManager2(context);
        sqLiteDatabase = dbManager.DBManager2();
        if (!isLoadAddress) {
            isLoadAddress = true;
            writeAddressData();
        }
    }

    private static void writeAddressData() {
        cbeans.clear();
        options1Items.clear();
        options2Items.clear();
        options3Items.clear();
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<DbBean> pbeans = dbManager.query(sqLiteDatabase, "0");
                cbeans.addAll(dbManager.query(sqLiteDatabase, "1"));
                List<DbBean> abeans = dbManager.query(sqLiteDatabase, "2");

                //1.省份列表
                options1Items = (ArrayList<DbBean>) pbeans;
                if (pbeans != null) {

                    for (int i = 0; i < pbeans.size(); i++) {  //遍历省份
                        ArrayList<DbBean> citys = new ArrayList<>(); //该省的城市列表（第二级）
                        ArrayList<ArrayList<DbBean>> province_areas = new ArrayList<>(); //该省的所有地区列表（第三极）

                        for (int j = 0; j < cbeans.size(); j++) {   //遍历找到该省份所有城市

                            if (cbeans.get(j).getParentid().equals(pbeans.get(i).getId())) {
                                citys.add(cbeans.get(j));
                                ArrayList<DbBean> city_areas = new ArrayList<>();  //该城市的所有地区列表
                                for (int k = 0; k < abeans.size(); k++) {  //该城市对应地区所有数据
                                    if (abeans.get(k).getParentid().equals(cbeans.get(j).getId())) {
                                        city_areas.add(abeans.get(k));
                                    }
                                }
                                province_areas.add(city_areas);
                            }
                        }
                        options2Items.add(citys);
                        options3Items.add(province_areas);
                    }
                }
                sqLiteDatabase.close();
                //地址数据库读取成功
                EventBus.getDefault().post(new EventBean(Constants.EventCode.ADDERESS_DB_READ_SUCCESS, null));
            }
        }).start();
    }


}
